Python中 rstrip() 的用法 以及和 strip() 的区别

您所在的位置:网站首页 python strip功能 Python中 rstrip() 的用法 以及和 strip() 的区别

Python中 rstrip() 的用法 以及和 strip() 的区别

2024-01-31 18:03| 来源: 网络整理| 查看: 265

1. 概述

在对数据进行预处理的时候,我们经常会用到 strip() 函数来去除字符串或者字段前后的空格,但其实还有一个函数 rstrip(),是用来删除字符串末尾的指定字符(默认为空格),那这两个函数有什么区别呢,rstrip()又怎么使用呢?

2. rstrip() 2.1 语法

str.rstrip([chars]) Python rstrip() 删除 string 字符串末尾的指定字符(默认为空格).

2.2 参数:

chars – 指定删除的字符(默认为空格) 返回: 返回删除 string 字符串末尾的指定字符后生成的新字符串。

2.3 实例 #!/usr/bin/env python3 # -*- coding: utf-8 -*- # @Time : 2019/3/2 20:32 # @Author : Arrow and Bullet # @FileName: rstrip().py # @Software: PyCharm # @Blog :https://blog.csdn.net/qq_41800366 str1 = " this is string example....wow!!! " str2 = str1.rstrip() # 默认是空格 print(str2) # this is string example....wow!!! str3 = "88888888this is string example....wow!!!8888888" str4 = str3.rstrip("8") print(str4) # 88888888this is string example....wow!!! str5 = str3.strip("8") print(str5) # this is string example....wow!!! 3. rstrip() 和 strip()的区别

Python strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。 Python rstrip() 删除 string 字符串末尾的指定字符(默认为空格).

希望能够帮助到大家,有什么问题可以 直接评论即可,喜欢有用的话可以点个赞让更多的人看到,如果不够详细的话也可以说,我会及时回复的。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3